import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


/**
 * Collection class of users that parses from a file.
 * 
 * @author Jay Fernandez
 * @author Efim Todorasco testing and corrections
 * @version May 31, 2011
 */
public class Users {
	
	/**
	 * List of instructors.
	 */
	private List<User> my_instructors = new ArrayList<User>();
	
	/**
	 * List of advisors.
	 */
	private List<User> my_advisors = new ArrayList<User>();
	
	/**
	 * List of students.
	 */
	private List<User> my_students = new ArrayList<User>();
	
	/**
	 * Constructor that initializes user from a file
	 * 
	 * @param the_file_location The file to be read from.
	 */
	public Users(String the_file_location) {
		doParse(the_file_location);
	}
	
	/**
	 * @return The list of Users.
	 */
	public List<User> getInstructors() {
		return my_instructors;
	}
	
	/**
	 * @return The list of Users.
	 */
	public List<User> getAdvisors() {
		return my_advisors;
	}
	
	/**
	 * @return The list of Users.
	 */
	public List<User> getStudents() {
		return my_students;
	}
	
	/**
	 * Main parse method that parses the file according
	 * to the various users.
	 * 
	 * @param users_prefs_csv The file to be read from.
	 */
	private void doParse(String users_prefs_csv) {
		BufferedReader reader;

		try {
			FileReader file = new FileReader(users_prefs_csv);
			reader = new BufferedReader(file);
			
			while (!reader.readLine().startsWith("#")) {
				//Keeps reading each line until it reaches a #
			}
			
			parseStudentPrefs(reader);
			parseTeacherPrefs(reader);
			parseAdvisorPrefs(reader);
			
		} catch (Exception e) {
			System.err.print(e);
		}
	}
	
	/**
	 * Parses the preferences of student users from a given csv file
	 * by adding new students to the users collection with their
	 * preferred courses and time slots indicated by the given file.
	 * 
	 * The csv file should be formatted per line in the following way:
	 * | Username | Course | Day | TimeStart | TimeEnd |.
	 * 
	 * @author Jay Fernandez (parsing concept by Efim)
	 * 
	 * @throws IOException 
	 * @param reader The buffered reader.
	 */
	private void parseStudentPrefs(BufferedReader reader) throws IOException {
		List<String> temp = new ArrayList<String>();
		String parsedLine;
		StringTokenizer tokenizer;
		
		parsedLine = reader.readLine();
		while (!parsedLine.startsWith("#")) {
			
			tokenizer = new StringTokenizer(parsedLine, ",");

			while (tokenizer.hasMoreTokens()) {
				temp.add(tokenizer.nextToken());
			}
			
			preference studPrefs = new preference(temp.get(1), temp.get(2), 
					Integer.parseInt(temp.get(3)), Integer.parseInt(temp.get(4)));
			User student = new User(temp.get(0));
			student.addPreference(studPrefs);
			my_students.add(student);
			
			temp.clear();
			parsedLine = reader.readLine();
		}
	}
	
	/**
	 * Parses the preferences of teacher users from a given csv file
	 * by adding new teachers to the users collection with their
	 * preferred courses and time slots indicated by the given file.
	 * 
	 * The csv file should be formatted per line in the following way:
	 * | Username | Course | Day | TimeStart | TimeEnd |.
	 * 
	 * @author Jay Fernandez
	 * 
	 * @throws IOException 
	 * @param reader The buffered reader.
	 */
	private void parseTeacherPrefs(BufferedReader reader) throws IOException {
		List<String> temp = new ArrayList<String>();
		String parsedLine;
		StringTokenizer tokenizer;
		
		parsedLine = reader.readLine();
		while (!parsedLine.startsWith("#")) {
			tokenizer = new StringTokenizer(parsedLine, ",");

			while (tokenizer.hasMoreTokens()) {
				temp.add(tokenizer.nextToken());
			}
			
			preference teachPrefs = new preference(temp.get(1), temp.get(2), 
					Integer.parseInt(temp.get(3)), Integer.parseInt(temp.get(4)));
			User teacher = new User(temp.get(0));
			teacher.addPreference(teachPrefs);
			my_instructors.add(teacher);
			
			temp.clear();
			parsedLine = reader.readLine();
		}
	}
	
	/**
	 * Parses the suggested classes of advisor users from a given csv file
	 * by adding new advisors to the users collection with their
	 * suggested courses indicated by the given file.
	 * 
	 * The csv file should be formatted per line in the following way:
	 * | Username | Course |.
	 * 
	 * @author Jay Fernandez
	 * 
	 * @throws IOException 
	 * @param reader The buffered reader.
	 */
	private void parseAdvisorPrefs(BufferedReader reader) throws IOException {
		List<String> temp = new ArrayList<String>();
		String parsedLine;
		StringTokenizer tokenizer;
			

		while ((parsedLine = reader.readLine()) != null) {
			tokenizer = new StringTokenizer(parsedLine, ",");

			while (tokenizer.hasMoreTokens()) {
				temp.add(tokenizer.nextToken());
			}
			preference advisorPrefs = new preference(temp.get(1), temp.get(2), 
					Integer.parseInt(temp.get(3)), Integer.parseInt(temp.get(4)));
			User advisor = new User(temp.get(0));
			advisor.addPreference(advisorPrefs);
			my_advisors.add(advisor);
			
			temp.clear();
		}
	}
}
